package com.test.deencredit.base_deencredit;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.ASBuilder_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.ASSycManager_deencredit;
import com.test.deencredit.util_deencredit.DecUtil_deencredit;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;

import org.jetbrains.annotations.NotNull;


public class BaseApplication_deencredit extends Application {

    private static BaseApplication_deencredit BaseApplication_deencredit;

    private String msg_deencredit;

    @NotNull
    public static BaseApplication_deencredit getInstant() {
        return BaseApplication_deencredit;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        BaseApplication_deencredit = this;
        try {
            //pie webview 适配
            initPieWebView_deencredit();

            //user info
            UserInfoUtil_deencredit.getInstance_deencredit().init_deencredit(getApplicationContext());

            //syc data sdk
            initSycDataSDK_deencredit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 适配android 9 网络进程
     */
    private void initPieWebView_deencredit() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName_deencredit(this);
            if (!DecUtil_deencredit.getPackageName_deencredit().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    private String getProcessName_deencredit(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
                if (processInfo.pid == android.os.Process.myPid()) {
                    return processInfo.processName;
                }
            }
        }
        return "";
    }

    public void initSycDataSDK_deencredit() {
        try {

            //tag2024
            ASBuilder_deencredit.setLog_deencredit(true);

            // 初始化
            ASSycManager_deencredit.getInstance_deencredit().init_deencredit(this);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMsg_deencredit(String msg_deencredit) {
        this.msg_deencredit = msg_deencredit;
    }

    public String getMsg_deencredit() {
        if (msg_deencredit == null) return "";
        final String fMSg = msg_deencredit;
        msg_deencredit = "";
        return fMSg;
    }

}
